package com.pan.hisystem.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pan.hisystem.pojo.*;
import com.pan.hisystem.service.*;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author qingyang
 * @since 2022-03-13
 */
@RestController
@RequestMapping("/registration")
public class RegistrationController {

    @Autowired
    private IRegistrationService registrationService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IOutpatientsService outpatientsService;

    @Autowired
    private IMedicalCardService medicalCardService;
    @Autowired
    private IPatientService patientService;

    @ApiOperation(value = "新办就诊卡")
    @PostMapping("/addMCard")
    public RespBean addMCard(@RequestBody MedicalCard medicalCard){
        return medicalCardService.addMCard(medicalCard);
    }

    @ApiOperation(value = "获取就诊卡")
    @GetMapping("/getMCard/{id}")
    public RespBean getMCard(@PathVariable String id){
        MedicalCard medicalCard=medicalCardService.getOne(new QueryWrapper<MedicalCard>().eq("id",id));
        if(medicalCard!=null){
            System.out.println(medicalCard.toString());
            return RespBean.success("获取成功!",medicalCard);
        }
        return RespBean.error("卡号错误，请重新输入!");
    }

    @ApiOperation(value = "根据卡号获取患者信息")
    @GetMapping(value = "/getPatientByMid/{mid}")
    public Patient getPatientByMid(@PathVariable String mid){
        Patient patient=patientService.getOne(new QueryWrapper<Patient>().eq("mid",mid));
        if(patient!=null){
            System.out.println(patient.toString());
            return patient;
        }
        return null;
    }

    @ApiOperation(value = "挂号")
    @PostMapping("/addRegistration")
    public RespBean addRegistration(@RequestBody Registration registration, Principal principal){
        System.out.println(registration.toString());
        if(null==principal){
            return RespBean.error("挂号员暂未登录,请登录!");
        }
        String username=principal.getName();//获取账号
        User user =userService.getOne(new QueryWrapper<User>().eq("username",username));
        registration.setRegisterId(user.getId());
        registration.setRegisteredTime(LocalDateTime.now());
        if(registration.getBackPrice()>=0){
            registration.setPayStatus(true);
        }
        else{
            registration.setPayStatus(false);
            return RespBean.error("挂号失败，实付金额不足!");
        }
        registration.setBackStatus(false);//默认不退号
        registration.setEnabled(false);//默认未就诊
        return registrationService.addRegistrationAndOutPatient(registration, user.getId());
    }

    @ApiOperation(value = "根据卡号获取挂号记录")
    @GetMapping("/getRecordByMid")
    public List<Registration> getRecordByMid(String mid){
        System.out.println(mid);
        return registrationService.getRecordByMid(mid);
    }
    @ApiOperation(value = "获取所有挂号记录")
    @GetMapping("/getAllRecords")
    public List<Registration> getAllRecords(){

        return registrationService.getRecordByMid(null);

    }

    @ApiOperation(value = "患者退号")
    @GetMapping("/backRegistration/{id}")
    public RespBean backRegistration(@PathVariable Integer id){
        Registration registration=registrationService.getById(id);
        if(registration.getEnabled()){
            return RespBean.error("退号失败!");
        }
        registration.setBackStatus(true);
        Outpatients outpatients=outpatientsService.getOne(new QueryWrapper<Outpatients>().eq("rid",registration.getId()));
        //先判断患者是否已去医生门诊就诊，若生成了处方单，则说明该患者已去就诊，无法退该挂号
        if(outpatients.getPreId()==null){
            if(registrationService.updateById(registration)){

                if(outpatientsService.updateById(outpatients)){
                    return RespBean.success("退号成功!");
                }

            }
            return RespBean.error("退号失败!");
        }
        return RespBean.error("退号失败!");
    }

    @ApiOperation(value = "获取角色为医生的操作员")
    @GetMapping("/getUserWithDoctor")
    public List<User> getUserWithDoctor(@RequestParam(value = "did") Integer did, @RequestParam(value = "tid") Integer tid, @RequestParam(value = "mid")String mid){
        return userService.getUserWithDoctor(did,tid,mid);
    }

}
